const START_YEAR: i32 = 2025;
const START_MONTH: i32 = 1;
pub fn retire_time(time: &str, tp: &str) -> String {
    let mut person_info = RetirementInfo::new(Gender::Male,2000,1,PrevRetirePlan::None);
    let parts: Vec<&str>= time.split("-").collect();
    let born_year = parts[0].parse::<i32>().unwrap();
    let born_month = parts[1].parse::<i32>().unwrap();
    person_info.born_year = born_year;
    person_info.born_month = born_month;

    if tp.starts_with("原"){
        person_info.gender = Gender::Female;
        let plan_age = &tp[21..23];
        if plan_age == "55".to_string() {
            person_info.prev_retire_plan = PrevRetirePlan::Plan1;
            person_info.calculate_retire_age();
        }
        else {
            person_info.prev_retire_plan = PrevRetirePlan::Plan2;
            person_info.calculate_retire_age();
        }
    }
    else {
        person_info.gender = Gender::Male;
        person_info.prev_retire_plan = PrevRetirePlan::None;
        person_info.calculate_retire_age();
    }

    if person_info.retire_age.fract() == 0.0 {
        format!("{},{},{}", person_info.retire_time, person_info.retire_age as i64, person_info.increment_month)
    }else {
        format!("{},{:.2},{}",person_info.retire_time,person_info.retire_age,person_info.increment_month)     
    }
   
}

enum Gender {
    Male,
    Female,
}

enum PrevRetirePlan {
    Plan1,//55
    Plan2,//50
    None,
}

struct RetirementInfo {
    gender: Gender,
    born_year: i32,
    born_month: i32,
    prev_retire_plan: PrevRetirePlan,
    retire_age: f64,
    increment_month: i32,
    retire_time: String,
}

impl RetirementInfo {
   fn new(gender: Gender, born_year: i32, born_month: i32, prev_retire_plan: PrevRetirePlan) -> Self {
       RetirementInfo {
           gender,
           born_year,
           born_month,
           prev_retire_plan,
           retire_age: 0.0,
           increment_month: 0,
           retire_time: "".to_string(),
       }
   }

   fn calculate_retire_age(&mut self) {
       match self.gender {
           Gender::Male => {
               if self.born_year < 1965 {
                   self.retire_age = 60.0;
                   self.increment_month = 0;
                   if self.born_month < 10{
                       self.retire_time = format!("{}-0{}",self.born_year+60,self.born_month);
                   }else {
                       self.retire_time = format!("{}-{}",self.born_year+60,self.born_month);
                   }
               }
               else if self.born_year > 1976 {
                   self.retire_age = 63.0;
                   self.increment_month = 36;
                   if self.born_month < 10{
                       self.retire_time = format!("{}-0{}",self.born_year+63,self.born_month);
                   }else {
                       self.retire_time = format!("{}-{}",self.born_year+63,self.born_month);
                   }
                }
               else {
                    let (prev_year,prev_month) = (self.born_year+60,self.born_month);
                    let mut  increment_month = prev_month + (prev_year - START_YEAR)*12 -START_MONTH;
                    increment_month = increment_month/4 + 1;
                    self.retire_age = 60.0 + (increment_month as f64)/12.0;
                    self.increment_month = increment_month;
                    let mut retire_month_now = prev_month + increment_month;
                    let retire_year_now = prev_year + retire_month_now / 12;
                    retire_month_now = retire_month_now % 12;
                    if retire_month_now < 10{
                        self.retire_time = format!("{}-0{}",retire_year_now,retire_month_now);
                    }
                    else {
                        self.retire_time = format!("{}-{}",retire_year_now,retire_month_now);
                    }
               }
           }
           Gender::Female => {
               match self.prev_retire_plan{
                   PrevRetirePlan::Plan1 => {
                        if self.born_year < 1970 {
                            self.retire_age = 55.0;
                            self.increment_month = 0;
                            if self.born_month < 10{
                                self.retire_time = format!("{}-0{}",self.born_year+55,self.born_month);
                            }
                            else {
                                self.retire_time = format!("{}-{}",self.born_year+55,self.born_month);
                            }
                        }
                        else if self.born_year > 1981 {
                            self.retire_age = 58.0;
                            self.increment_month = 36;
                            if self.born_month < 10{
                                self.retire_time = format!("{}-0{}",self.born_year+58,self.born_month);
                            }else {
                                self.retire_time = format!("{}-{}",self.born_year+58,self.born_month);
                            }
                        }
                        else {
                            let (prev_year,prev_month) = (self.born_year+55,self.born_month);
                            let mut  increment_month = prev_month + (prev_year - START_YEAR)*12 -START_MONTH;
                            increment_month = increment_month/4 + 1;
                            self.retire_age = 55.0 + (increment_month as f64)/12.0;
                            self.increment_month = increment_month;
                            let mut retire_month_now = prev_month + increment_month;
                            let retire_year_now = prev_year + retire_month_now / 12;
                            retire_month_now = retire_month_now % 12;
                            if retire_month_now < 10{
                                self.retire_time = format!("{}-0{}",retire_year_now,retire_month_now);
                            }
                            else {
                                self.retire_time = format!("{}-{}",retire_year_now,retire_month_now);
                            }
                            
                        }
                   }
                   PrevRetirePlan::Plan2 => {
                       if self.born_year < 1975 {
                           self.retire_age = 50.0;
                           self.increment_month = 0;
                           if self.born_month < 10{
                               self.retire_time = format!("{}-0{}",self.born_year+50,self.born_month);
                           }
                           else {
                            self.retire_time = format!("{}-{}",self.born_year+50,self.born_month);
                           }
                       }
                       else if self.born_year > 1984 {
                           self.retire_age = 55.0;
                           self.increment_month = 60;
                           if self.born_month < 10{
                               self.retire_time = format!("{}-0{}",self.born_year+55,self.born_month);
                           }else {
                               self.retire_time = format!("{}-{}",self.born_year+55,self.born_month);
                           }
                       }
                       else {
                           let (prev_year,prev_month) = (self.born_year+50,self.born_month);
                           let mut  increment_month = prev_month + (prev_year - START_YEAR)*12 -START_MONTH;
                           increment_month = increment_month/2 + 1;
                           self.retire_age = 50.0 + (increment_month as f64)/12.0;
                           self.increment_month = increment_month;
                           let mut retire_month_now = prev_month + increment_month;
                           let retire_year_now = prev_year + retire_month_now / 12;
                           retire_month_now = retire_month_now % 12;
                           if retire_month_now < 10{
                               self.retire_time = format!("{}-0{}",retire_year_now,retire_month_now);
                           }else {
                               self.retire_time = format!("{}-{}",retire_year_now,retire_month_now);
                           }
                       }
                       
                   }
                   _ => {
                       panic!("Invalid prev retire plan")
                   }
               }
           }
       }
   }
}

